import router from '@/router';
// 进度条
import NProgress from 'nprogress';
// 进度条样式
import 'nprogress/nprogress.css';
import { getCookie } from '@/utils/cookie';
import { DEMO_AUTH_TOKEN_FRONT } from '@/common/constant';

NProgress.configure({ showSpinner: false });

// 不需要token的白名单
const whiteList = ['/login'];

// 注册路由跳转的权限控制
router.beforeEach(async (to, from, next) => {
  NProgress.start();

  // // ------------------------------------
  //   // ！！！记得改router里的redirect: "/home"！！！
  // next();
  // NProgress.done();
  // // ------------------------------------

  const hasToken = getCookie(DEMO_AUTH_TOKEN_FRONT);
  if (hasToken && hasToken !== 'undefined') {
    if (to.path === '/login') {
      next({ path: '/' });
      console.log('回到登录了');
      NProgress.done();
    } else {
      next();
    }
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      console.log('feilogin');
      next('/login');
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});
